package com.example.chenjun.myapplication.adapter;

import android.database.DataSetObserver;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.chenjun.myapplication.R;
import com.example.chenjun.myapplication.bean.LabelBean;

import java.util.List;

public class CookExpandListAdapter implements ExpandableListAdapter {
    private List<LabelBean> mLabelBeanList;
    private DataSetObserver mObserver;

    @Override
    public void registerDataSetObserver(DataSetObserver observer) {
        mObserver = observer;
    }

    @Override
    public void unregisterDataSetObserver(DataSetObserver observer) {

    }

    @Override
    public int getGroupCount() {
        return mLabelBeanList == null ? 0 : mLabelBeanList.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        List<LabelBean.ListBean> childList = mLabelBeanList.get(groupPosition).getList();
        return childList == null ? 0 : childList.size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return mLabelBeanList.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return mLabelBeanList.get(groupPosition).getList().get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return (long) (groupPosition * Math.pow(10, String.valueOf(childPosition).length()) + childPosition);
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        if (convertView == null){
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.app_list_item_group_cook_label, parent, false);
        }
        TextView tvGroupContent = convertView.findViewById(R.id.tv_group_content);
        tvGroupContent.setText(mLabelBeanList.get(groupPosition).getName());
        ImageView ivRight = convertView.findViewById(R.id.iv_right);
        if (isExpanded){
            ivRight.setImageResource(R.mipmap.down_arrow);
        }else {
            ivRight.setImageResource(R.mipmap.right_arrow);
        }
        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        if (convertView == null){
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.app_list_item_child_cook_label, parent, false);
        }
        TextView tvGroupContent = convertView.findViewById(R.id.tv_group_content);
        tvGroupContent.setText(mLabelBeanList.get(groupPosition).getList().get(childPosition).getName());
        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return false;
    }

    @Override
    public boolean areAllItemsEnabled() {
        return false;
    }

    @Override
    public boolean isEmpty() {
        return mLabelBeanList == null || mLabelBeanList.isEmpty();
    }

    @Override
    public void onGroupExpanded(int groupPosition) {
    }

    @Override
    public void onGroupCollapsed(int groupPosition) {

    }

    @Override
    public long getCombinedChildId(long groupId, long childId) {
        return 0;
    }

    @Override
    public long getCombinedGroupId(long groupId) {
        return 0;
    }

    public void setData(List<LabelBean> list){
        mLabelBeanList = list;
        mObserver.onInvalidated();
    }
}
